/**
 * 定义ui公共模块
 */
(function(window, angular, undefined){

    var module = angular.module('water.common.EcharService',[]);

    module.service('$EcharService',function($q){

        /**
         * 渲染单条数据
         *
         * domid:'char_div'
         * 用于渲染char的元素id		 *
         *
         * title:'COD测量值'
         * char的标题
         *
         * xTimes:['周一','周二','周三','周四','周五','周六','周日']
         * 横坐标的时间集合
         *
         * yValues:{
         * 		name:'COD',
         * 		data:[11, 11, 15, 13, 12, 13, 10]
         * }
         * 业务数据
         *
         */
        this.showSingleSeries = function(domid,title,xTimes,yValues){

            var legends = [];

            for(var i = 0; i<yValues.length; i++){
                legends.push(yValues[i].name);
            }

            var min = this.getMinChar(yValues.data);

            var option = {
                title: {
                    text: title
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data:legends
                },
                xAxis:  {
                    type: 'category',
                    boundaryGap: false,
                    data: xTimes
                },
                yAxis: {
                    type: 'value',
                    min: min,
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                series: [
                    {
                        name : yValues.name,
                        type:'line',
                        data:yValues.data,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };

            var myChart = echarts.init(document.getElementById(domid));
            myChart.setOption(option);
        }

        /**
         * 渲染多条数据
         *
         */
        this.showMultipleSeries = function(domid,title,xTimes,yValues){

            var legends = [];

            for(var i = 0; i<yValues.length; i++){
                var obj = {};
                obj.name = yValues[i].name;
                obj.textStyle = "#DC143C";
                legends.push(yValues[i].name);
            }

            var unit = 'mg/L';

            var arrYAxis = [];
            for(var i = 0; i<yValues.length; i++){
                var data = [];
                for(var j=0;j<yValues[i].data.length;j++){
                    if(yValues[i].data[j]!=undefined){
                        data.push(yValues[i].data[j]);
                    }
                }
                var min = this.getMinChar(data);
                var max = this.getMaxChar(data);

                var yAxis = {};
                yAxis.type = 'value';
                yAxis.min = 0;
                yAxis.max = max;
                yAxis.interval = (max)/5;
                yAxis.axisLabel = {formatter: '{value} '+unit};
                yAxis.axisLabel = {formatter: function(value){
                        return Number(value).toFixed(2) + unit;
                    }
                };
                if(i==0){
                    yAxis.offset= 0;
                    yAxis.axisLine = {
                        lineStyle:{
                            color:'#1E90FF',
                            width:0
                        }
                    };
                }
                if(i==1){
                    yAxis.offset= 0;
                    yAxis.axisLine = {
                        lineStyle:{
                            color:'#00FF7F',
                            width:0
                        }
                    };
                }
                if(i==2){
                    yAxis.offset= 75;
                    yAxis.axisLine = {
                        lineStyle:{
                            color:'#FFA500',
                            width:0
                        }
                    };
                }


                arrYAxis.push(yAxis);
            }

            var arrSeries = [];
            for(var i = 0; i<yValues.length; i++){
                var series = {};
                series.name = yValues[i].name;
                series.type = 'line';
                series.data = yValues[i].data;
                series.smooth = true;
                var markPoint = {};
                markPoint.data = [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ];
                series.markPoint = markPoint;
                if(i==0){
                    series.itemStyle = {
                        normal : {
                            color: "#1E90FF",//图例的颜色
                            lineStyle:{
                                color:'#1E90FF'
                            }
                        }
                    };
                }
                if(i==1){
                    series.itemStyle = {
                        normal : {
                            color: "#00FF7F",//图例的颜色
                            lineStyle:{
                                color:'#00FF7F'
                            }
                        }
                    };
                }
                if(i==2){
                    series.itemStyle = {
                        normal : {
                            color: "#FFA500",//图例的颜色
                            lineStyle:{
                                color:'#FFA500'
                            }
                        }
                    };
                }

                series.yAxisIndex = i;
                arrSeries.push(series);
            }

            var option = {
                title: {
                    text: title
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data:legends
                },
                xAxis:  {
                    type: 'category',
                    boundaryGap: false,
                    data: xTimes
                }
            };
            option.series = arrSeries;
            option.yAxis = arrYAxis;

            var myChart = echarts.init(document.getElementById(domid));
            myChart.setOption(option);
        }

        /**
         * 获取一组数据的最小值
         * data:[1,2,3,4,5,6,7]
         */
        this.getMinChar = function(data){

            var min = data[0];

            for(var i=0;i<data.length;i++){
                var value = data[i];
                if(value < min){
                    min = value;
                }
            }

            return min;
        }


        /**
         * 获取一组数据的最大值
         * data:[1,2,3,4,5,6,7]
         */
        this.getMaxChar = function(data){

            var max = data[0];

            for(var i=0;i<data.length;i++){
                var value = data[i];
                if(value > max){
                    max = value;
                }
            }

            return max;
        }

        /**
         * 测试方法
         */
        this.test = function(){
            var title = "COD测量值";
            var times = ['周一','周二','周三','周四','周五','周六','周日'];
            var value = {
                name:'COD',
                data:[11, 11, 15, 13, 12, 13, 10]
            };

            this.showSingleSeries('char_tb',title,times,value);
        }

    });

}(window, angular));